package view;

import Dao.UserDao;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PasswordChange extends JFrame {
    private JTextField username;
    private JTextField password1;
    private JTextField password2;
    private String name;

    public static void main(String[] args) {
        new PasswordChange("");
    }

    public PasswordChange(String name) {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.name = name;
        setTitle("\u66F4\u6539\u5BC6\u7801");
        setBounds(100, 100, 348, 272);

        JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D");

        username = new JTextField(name);
        username.setEnabled(false);
        username.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("\u65B0\u5BC6\u7801");

        password1 = new JPasswordField();
        password1.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("\u786E\u8BA4\u5BC6\u7801");

        password2 = new JPasswordField();
        password2.setColumns(10);

        JButton btnNewButton = new JButton("\u786E\u8BA4\u4FEE\u6539");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                PasswordChangeActionPerformed(e);
            }
        });
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(53)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(lblNewLabel)
                                        .addComponent(lblNewLabel_2))
                                .addGap(26)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addComponent(btnNewButton)
                                        .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
                                                .addComponent(password2)
                                                .addComponent(username, Alignment.LEADING)
                                                .addComponent(password1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)))
                                .addContainerGap(184, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(29)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(username, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(28)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(password1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(24)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_2)
                                        .addComponent(password2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(48)
                                .addComponent(btnNewButton)
                                .addContainerGap(55, Short.MAX_VALUE))
        );
        getContentPane().setLayout(groupLayout);

    }

    private void PasswordChangeActionPerformed(ActionEvent e) {
        String username = name;
        String pwd = password2.getText();
        int count = -1;
        if (password1.getText().isEmpty() || password2.getText().isEmpty()){
            JOptionPane.showMessageDialog(null,"请输入新密码");
            return;
        }
        if (password1.getText().equals(password2.getText())){
            count = UserDao.ChangePassword(username,pwd);
        } else {
            JOptionPane.showMessageDialog(null,"密码不一致");
            return;
        }
        if (count >= 0 ){
            JOptionPane.showMessageDialog(null,"修改成功");
        } else {
            JOptionPane.showMessageDialog(null,"修改失败");
        }
    }
}
